package team.se.tms.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import team.se.tms.annotation.SpecialCheck;
import team.se.tms.constant.PathConstant;
import team.se.tms.dao.Dao;
import team.se.tms.dto.CourseMaterialFile;
import team.se.tms.dto.Page;
import team.se.tms.dto.ParameterMap;
import team.se.tms.dto.ParameterPage;
import team.se.tms.exception.DeleteException;
import team.se.tms.exception.InsertException;
import team.se.tms.exception.ParamException;
import team.se.tms.factory.ModelFactory;
import team.se.tms.po.CourseMaterialInfo;
import team.se.tms.po.TeachCourse_CourseMaterialInfo;
import team.se.tms.service.CourseMaterialInfoService;
import team.se.tms.util.FileUtil;
import team.se.tms.util.KeyUtil;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 课程资料信息业务层，具体实现课程资料信息业务接口层定义的方法
 *
 * @author winkyle
 * @date 2018/11/27
 */
@Slf4j
@Service("CourseMaterialInfoServiceImpl")
public class CourseMaterialInfoServiceImpl extends BaseServiceImpl implements CourseMaterialInfoService {

    @Resource(name = "DaoImpl")
    private Dao dao;

    /**
     * 获取全部课程资料信息
     *
     * @param page 分页
     * @return 课程资料信息列表
     * @throws Exception 异常
     * @create on 2018/11/27 by winkyle
     */
    @Override
    public List<CourseMaterialFile> listCourseMaterialFiles(Page page) throws Exception {
        return FileUtil.analysisCourseMaterialFiles(dao.selectList("CourseMaterialInfoMapper.selectCourseMaterialFiles",getParameterPage(page)));
    }

    /**
     * 根据授课id获取课程资料信息
     *
     * @param teachCourseId 授课id
     * @param page 分页
     * @return 课程资料信息列表
     * @throws Exception 异常
     * @create on 2018/11/27 by winkyle
     */
    @Override
    public List<CourseMaterialFile> listCourseMaterialFiles(@SpecialCheck("Pk")String teachCourseId, Page page) throws Exception {
        return FileUtil.analysisCourseMaterialFiles(dao.selectList("CourseMaterialInfoMapper.selectCourseMaterialFilesByTeachCourseId",getParameterPage(page,"teachCourseId",teachCourseId)));
    }

    /**
     * 根据授课id存储课程资料文件，保存课程资料信息
     *
     * @param teachCourseId 授课id
     * @param multipartFile 课程资料文件
     * @return 保存结果
     * @throws Exception 异常
     * @create on 2018/11/27 by winkyle
     */
    @Override
    public Boolean saveCourseMaterial(@SpecialCheck("Pk")String teachCourseId, MultipartFile multipartFile) throws Exception {
        if(multipartFile==null){
            throw new ParamException("文件不能为空");
        }
        Date createTime = new Date();
        CourseMaterialInfo courseMaterialInfo;
        try{
            courseMaterialInfo = ModelFactory.createCourseMaterialInfo(multipartFile,createTime);
        }catch (Exception e){
            throw new InsertException("存储课程资料文件失败");
        }
        int insertResult = dao.insert("CourseMaterialInfoMapper.insertCourseMaterialInfo",courseMaterialInfo);
        if (insertResult<=0){
            throw new InsertException();
        }
        TeachCourse_CourseMaterialInfo teachCourse_courseMaterialInfo = ModelFactory.createTeachCourse_CourseMaterialInfo(teachCourseId,courseMaterialInfo.getCourseMaterialId(),createTime);
        insertResult = dao.insert("TeachCourse_CourseMaterialInfoMapper.insertTeachCourse_CourseMaterialInfo",teachCourse_courseMaterialInfo);
        if(insertResult<=0){
            throw new InsertException();
        }
        return true;
    }

    /**
     * 根据授课id，课程资料id，是否删除文件删除课程资料文件，删除课程资料信息
     *
     * @param teachCourseId 授课id
     * @param courseMaterialIds 课程资料id
     * @param deleteFile 是否删除文件
     * @return 删除结果
     * @throws Exception 异常
     * @create on 2018/11/27 by winkyle
     */
    @Override
    public Boolean removeCourseMaterials(@SpecialCheck("Pk")String teachCourseId, @SpecialCheck("Pk")List<String> courseMaterialIds, Boolean deleteFile) throws Exception {
        int deleteResult = 0;
        if(deleteFile) {
            List<CourseMaterialInfo> courseMaterialInfos;
            try {
                courseMaterialInfos = dao.selectList("CourseMaterialInfoMapper.selectCourseMaterialInfosByCourseMaterialIds", courseMaterialIds);
            }catch (Exception e){
                throw new DeleteException("无法删除不存在的课程资料");
            }
            try {
                for (CourseMaterialInfo courseMaterialInfo : courseMaterialInfos) {
                    FileUtil.destroyFile(courseMaterialInfo.getCourseMaterialAddress());
                }
            }catch (Exception e){
                throw new DeleteException("删除课程资料文件失败");
            }
            log.debug("courseMaterialInfos有{}条",courseMaterialInfos==null?0:courseMaterialInfos.size());
            deleteResult = dao.delete("CourseMaterialInfoMapper.deleteCourseMaterialInfosByCourseMaterialIds", courseMaterialIds);
            if(deleteResult<=0){
                throw new DeleteException();
            }
        }
        ParameterMap parameterMap = getParameterMap();
        parameterMap.put("courseMaterialIds",courseMaterialIds);
        parameterMap.put("teachCourseId",teachCourseId);
        deleteResult = dao.delete("TeachCourse_CourseMaterialInfoMapper.deleteTeachCourse_CourseMaterialInfosByTeachCourseIdAndCourseMaterialIds", parameterMap);
        if(deleteResult<=0){
            throw new DeleteException();
        }
        return true;
    }

    /**
     * 云端上传
     * @create on 2018/12/28 by argentum
     *
     * @param teachCourseCourseMaterialInfos
     * @return
     * @throws Exception
     */
    @Override
    public Boolean saveTeachCourse_CourseMaterialInfos(List<TeachCourse_CourseMaterialInfo> teachCourseCourseMaterialInfos) throws Exception {
        Date createTime = new Date();
        for(int i = 0; teachCourseCourseMaterialInfos.size()>0;i++){
            teachCourseCourseMaterialInfos.get(i).setTeachCourseMaterialCreateTime(createTime);
        }
        int count = dao.insert("insertTeachCourse_CourseMaterialInfos",teachCourseCourseMaterialInfos);
        if(count<=0){
            throw new InsertException();
        }
        return true;
    }

}
